<template>
  <div class="wrapLimit clx main">
    <div class="inventory_control">
      <div class="form-baseinfo">
        <el-form ref="inputModel" :model="inputModel" :inline="true" label-width="80px" class="form-baseinfo">
          <el-row>
            <el-col :span="24">
              <p class="title">库存调整</p>
            </el-col>
          </el-row>
          <el-form-item label="物料类型">
            <el-select v-model="inputModel.centreType" filterable @change="centreTypeChange">
              <el-option v-for="item in centreType" :key="item.label" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="物料名称">
            <el-select v-model="inputModel.pTitle" filterable>
              <el-option v-for="item in types" :key="item.id" :label="item.pTitle" :value="item.pTitle" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="insertData">添加</el-button>
          </el-form-item>
          <LPTablePage
            :show-index="true"
            :show-indexfixed="false"
            index-label="序号"
            :table-data="tableData"
            :table-column="tableColumn"
            :show-page="false"
            align="center"
          >
            <template slot="pCount" slot-scope="{ row }">
              <el-input-number v-model="row.pCount" />
            </template>
          </LPTablePage>
          <el-form-item>
            <span>注：库存变更，为正，则为入库库存，为负，则为出库库存。</span>
          </el-form-item>
        </el-form>
        <div class="form-baseinfo">
          <el-button size="small" type="primary" @click="back">关闭</el-button>
          <el-button size="small" type="primary" @click="Save">保存</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCategoryInfo } from '@/api/LaborProtection/categoryInfo'
import { getProductList } from '@/api/LaborProtection/product'
import { getProductInventoryList, UpdateProductInventory } from '@/api/LaborProtection/productInventory'
import LPTablePage from '@/components/LPTablePage/index.vue'

export default {
  name: 'InventoryControl',
  components: { LPTablePage },
  data() {
    return {
      inputModel: {
        centreType: '',
        pTitle: ''
      },
      // 列表数据
      tableData: [],
      // 表格字段
      tableColumn: [
        { label: '物料类型', prop: 'pClassName' },
        { label: '物料编码', prop: 'pNo' },
        { label: '物料名称', prop: 'pTitle', width: 330 },
        { label: '计量单位', prop: 'pUnit' },
        { label: '劳保库存', prop: 'plbQuantity' },
        { label: '劳保库存变更', prop: 'pCount', render: true, width: 300 }
      ],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      types: [],
      bigType: [],
      centreType: [],
      smallType: []
    }
  },
  mounted() {
    this.getCategoryInfo(2)
  },
  methods: {
    setDropDownStructure(maps) {
      return maps.map((item) => {
        return {
          label: item.cName,
          value: item.categoryNo
        }
      })
    },
    /**
     *  1：大类 2：中类 3：小类
     * @param {*} type
     */
    getCategoryInfo(type) {
      const data = {
        CCLass: type,
        SkipCount: 0
      }
      getCategoryInfo(data).then((res) => {
        if (!res.error) {
          if (type == 1) {
            this.bigType = res.items
          } else if (type == 2) {
            this.centreType = this.setDropDownStructure(res.items)
          } else {
            this.smallType = this.setDropDownStructure(res.items)
          }
        }
      })
    },
    getProductList(QCategoryNo) {
      const data = {
        QCategoryNo: QCategoryNo,
        SkipCount: 0
      }
      getProductList(data).then((res) => {
        this.types = res.items
      })
    },
    back() {
      this.$router.push({
        path: './index'
      })
    },
    Save() {
      const data = {
        childList: []
      }
      this.tableData.forEach((item) => {
        data.childList.push({
          pNo: item.pNo,
          pTitle: item.pTitle,
          pCount: item.pCount
        })
      })
      UpdateProductInventory(data).then((res) => {})
    },
    centreTypeChange(v) {
      this.getProductList(v)
      this.inputModel.pTitle = ''
    },
    insertData() {
      let falg = false
      this.tableData.forEach((item) => {
        if (item.pTitle == this.inputModel.pTitle) {
          falg = true
        }
      })
      if (falg) {
        return this.$message.error('不可重复添加同一物料')
      }
      const params = {
        pTitle: this.inputModel.pTitle,
        SkipCount: 0
      }
      getProductInventoryList(params).then((res) => {
        if (!res.error) {
          res.items.forEach((item) => {
            item.pCount = 0
            this.tableData.push(item)
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
.el-form-item__label {
  font-weight: normal;
}

.el-dialog__body {
  padding-top: 0px;
}

.el-divider {
  margin-top: 5px;
}
</style>

<style lang="scss" scoped>
.inventory_control {
  .form-baseinfo {
    border: 1px solid #dcdfe6;
    padding: 10px;

    ::v-deep .search-bar input.el-input__inner {
      width: 2rem;
    }

    .title {
      font-size: 16px;
      font-weight: 600;
    }

    ::v-deep .el-form-item {
      padding-left: 20px;
      margin-bottom: 10px;
    }

    .btn_area {
      padding-left: 0.1rem;
      padding-bottom: 0.1rem;
    }
  }
}
</style>
